<?php

namespace framework\system\database;

class dbSqlString extends dbObject {

    public $sqlString = "";
    public $sqlStrings = array();
    private $dist = false;
    private $option = "select";
    private $condition_array = array();
    private $expression = array();
    private $tables = array();
    private $cv = array();

    public function toString() {

        /*
         * 生成语句
         */
        $this->generate();
        /*
         * 替换前缀
         */
        if (defined('DB_PREFIX')) {
            $this->sqlString = str_replace('#@', DB_PREFIX, $this->sqlString);
        } else {
            $this->sqlString = str_replace('#@', '', $this->sqlString);
        }
        return $this->sqlString;
    }

    public function init() {
        $this->clean();
    }

    private function clean() {
        $this->expression = array();
        $this->condition_array = array();
        $this->tables = array();
        $this->cv = array();
    }

    public function establishStart($flag) {

        $this->sqlString[$flag] = NULL;
    }

    public function establishEnd() {
        
    }

    private function option() {
        return $this->option;
    }

///////////////////////////////////////////////////////////////////////////////
    //SET
    //SELECT INSERT UPDATE DELETE
    //function start
    ///////////////////////////////////////////////////////////////////////////////   
    public function setSelect($column, $dist = FALSE) {

        $this->option = "SELECT";
        $this->dist = $dist;

        $this->expression[] = " " . $column . " ";
    }

    public function setInsert($column, $value = NULL, $format = "%s") {
        $cv = [$column, $format, $value];
        $this->cv[] = $cv;
        $this->option = "INSERT";
    }

    public function setUpdate($column, $value = NULL, $format = "%s") {
        $cv = [$column, $format, $value];
        $this->cv[] = $cv;
        $this->option = "UPDATE";
    }

    public function setDelete() {

        $this->option = "DELETE";
    }

///////////////////////////////////////////////////////////////////////////////
    //SET
    //SELECT INSERT UPDATE DELETE
    //function end
    ///////////////////////////////////////////////////////////////////////////////   
    public function setTable($tableArray = null) {
        if (is_string($tableArray)) {

            $this->tables[] = trim($tableArray);
        } elseif (is_array($tableArray)) {
            foreach ($tableArray as $key => $value) {
                $this->tables[] = trim($value);
            }
        }
    }

    private function expression() {

        $expression = $this->expression;
        $SQL = $expression[0];
        for ($i = 1; !empty($expression[$i]); $i++) {
            $SQL.="," . $expression[$i];
        }
        return $SQL;
    }

    public function setC($how, $conditionFormat, $value = null) {
        $this->setCondition($how, $conditionFormat, $value);
    }

    public function setCondition($how, $conditionFormat, $value = null) {
        $this->condition_array[] = array($how, $conditionFormat, $value);
    }

    ///////////////////////////////////////////////////////////////////////////
    //
    //开始生成SQL语句
    //
    ///////////////////////////////////////////////////////////////////////////
    private function generate() {
        $SQL = "";
        if (strtoupper($this->option) == "UPDATE") {
            $SQL = $this->option() . $this->Tables();

            $SQL = $SQL . " SET ";

            $SQL = $SQL . $this->CV();

            $SQL = $SQL . $this->Condition();
            $this->sqlString = $SQL;
            return;
        }
        if (strtoupper($this->option) == "SELECT") {
            $SQL = $this->option();
            if ($this->dist) {
                $SQL = $SQL . " DISTINCT";
            }
            $SQL = $SQL . $this->expression();
            $SQL = $SQL . " FROM ";
            $SQL = $SQL . $this->Tables();
            $SQL = $SQL . $this->Condition();
            $this->sqlString = $SQL;
            return;
        }
        if (strtoupper($this->option) == "INSERT") {

            $SQL = $this->option();
            $SQL = $SQL . " INTO ";
            $SQL = $SQL . $this->Tables();

            $SQL = $SQL . $this->CV();

            $SQL = $SQL . $this->Condition();
            $this->sqlString = $SQL;
            return;
        }
        if (strtoupper($this->option) == "DELETE") {

            $SQL = $this->option();
            $SQL = $SQL . " FROM ";
            $SQL = $SQL . $this->Tables();

            $SQL = $SQL . $this->Condition();
            $this->sqlString = $SQL;
            return;
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    //
    //结束生成SQL语句
    //
    ///////////////////////////////////////////////////////////////////////////
    protected function CV() {
        $values_array = $this->cv;
        $type = $this->option;
        if (strtolower($type) == "insert") {

            $SQL = "(" . $values_array[0][0];
            for ($i = 1; !empty($values_array[$i]); $i++) {
                $SQL.="," . $values_array[$i][0];
            }
            $SQL.=")VALUES('" . sprintf($values_array[0][1], $values_array[0][2]);
            for ($i = 1; !empty($values_array[$i]); $i++) {
                $SQL.="','" . sprintf($values_array[$i][1], $values_array[$i][2]);
            }
            $SQL.="')";
        }
        if (strtolower($type) == "update") {
            $SQL = $values_array[0][0] . " = '" . sprintf($values_array[0][1], $values_array[0][2]);
            for ($i = 1; !empty($values_array[$i]); $i++) {
                $SQL.="'," . $values_array[$i][0] . " = '" . sprintf($values_array[$i][1], $values_array[$i][2]);
            }
            $SQL = $SQL . "'";
        }
        return $SQL;
    }

    protected function Condition() {
        $condition_array = $this->condition_array;
        $SQL = "";
        /**
         * 
         * 
         */
        for ($i = 0; !empty($condition_array[$i]); $i++) {
            if (strtolower($condition_array[$i][0]) == "join") {
                $SQL.= " " . sprintf($condition_array[$i][1], $condition_array[$i][2]);
            }
        }
        /**
         * 
         * 
         */
        for ($i = 0; !empty($condition_array[$i]); $i++) {
            if (strtolower($condition_array[$i][0]) == "where") {
                $SQL.= " WHERE " . sprintf($condition_array[$i][1], $condition_array[$i][2]);
            }
        }

        /**
         * 
         * 
         */
        for ($i = 0; !empty($condition_array[$i]); $i++) {
            if (strtolower($condition_array[$i][0]) == "and") {
                $SQL.= " AND " . sprintf($condition_array[$i][1], $condition_array[$i][2]);
            }
        }

        /**
         * 
         * 
         */
        for ($i = 0; !empty($condition_array[$i]); $i++) {
            if (strtolower($condition_array[$i][0]) == "or") {
                $SQL.= " OR " . sprintf($condition_array[$i][1], $condition_array[$i][2]);
            }
        }

        /**
         * 
         * 
         */
        for ($i = 0; !empty($condition_array[$i]); $i++) {
            if (strtolower($condition_array[$i][0]) == "not") {
                $SQL.= " NOT " . sprintf($condition_array[$i][1], $condition_array[$i][2]);
            }
        }

        /**
         * $sqlString->setC("order by","%s","ID DESC");
         * 
         */
        for ($i = 0; !empty($condition_array[$i]); $i++) {
            if (strtolower($condition_array[$i][0]) == "order by") {
                $SQL.= " ORDER BY " . sprintf($condition_array[$i][1], $condition_array[$i][2]);
            }
        }

        /**
         * 
         * 
         */
        for ($i = 0; !empty($condition_array[$i]); $i++) {
            if (strtolower($condition_array[$i][0]) == "limit") {
                $SQL.= " Limit " . sprintf($condition_array[$i][1], $condition_array[$i][2]);
            }
        }
//$SQL = "WHRER ID = 1";
        return " " . $SQL;
    }

    public function Tables() {
        $tables = $this->tables;
        $SQL = $tables[0];
        for ($i = 1; !empty($tables[$i]); $i++) {
            $SQL.="," . $tables[$i];
        }
        return " " . $SQL . " ";
    }

}
